package com.ruoyi.web.controller.system;

import com.ruoyi.camera.domain.TokenManager;
import com.ruoyi.common.annotation.Anonymous;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.camera.service.ApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.Objects;
import com.ruoyi.camera.domain.CustomApiException;


@RestController
@RequestMapping("/system/api")
public class CameraProxyController {

    private final ApiService apiService;
    private final TokenManager tokenManager;
    private static final Logger logger = LoggerFactory.getLogger(CameraProxyController.class);

    @Autowired
    public CameraProxyController(ApiService apiService, TokenManager tokenManager) {
        this.apiService = apiService;
        this.tokenManager = tokenManager;
    }

    /**
     * 获取token
     * * @author xhl
     */
    @GetMapping("/getToken")
    public ResponseEntity<Map<String, Object>> getToken() {
        try {
            Map<String, Object> response = apiService.getToken();
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(Map.of("error", "Failed to get token"));
        }
    }

    /**
     * 主机维护与管理
     * * @author xhl
     * @return
     */
    @GetMapping("/queryDeviceStatus")   //主机状态查询
    public ResponseEntity<Map<String, Object>> queryDeviceStatus() {
        try {
            // 直接从 TokenManager 获取 token
            String token = tokenManager.getToken();

            // 使用获取到的 token 查询设备状态
            Map<String, Object> deviceStatusResponse = apiService.queryDeviceStatus(token);

            // 检查设备状态响应是否为空
            if (deviceStatusResponse == null) {
                throw new CustomApiException("Device status response is null.");
            }

            return ResponseEntity.ok(deviceStatusResponse);
        } catch (CustomApiException e) {
            // 记录错误信息
            logger.error("Error occurred while querying device status: {}", e.getMessage());
            // 返回带有适当 HTTP 状态码和错误消息的响应
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            logger.error("Unexpected error occurred: ", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("error", "An unexpected error occurred."));
        }
    }
    /**
     * 摄像机管理
     * * @author xhl
     * @return
     */

    @Anonymous
    @GetMapping("/queryCameraChannel")    //相机通道号查询
    public ResponseEntity<Map<String, Object>> queryCameraChannel(@RequestParam(required = false) Integer cameraType) {
        try {
            // 直接从 TokenManager 获取 token
            String token = tokenManager.getToken();
            // 使用获取到的 token 查询相机通道号
            Map<String, Object> cameraChannelResponse = apiService.queryCameraChannel(token, cameraType);
            // 检查相机通道号响应是否为空
            if (cameraChannelResponse == null) {
                throw new CustomApiException("相机通道号响应为空.");
            }
            return ResponseEntity.ok(cameraChannelResponse);
        } catch (CustomApiException e) {
            logger.error("查询相机通道号时出错: {}", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            logger.error("发生意外错误: ", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("error", "An unexpected error occurred."));
        }
    }
    @Anonymous
    @GetMapping("/queryCameraList") //相机列表查询
    public ResponseEntity<Map<String, Object>> queryCameraList(@RequestParam(required = false) Integer cameraType, String cameraId) {
        try {
            // 直接从 TokenManager 获取 token
            String token = tokenManager.getToken();
            // 使用获取到的 token 查询相机信息
            Map<String, Object> cameraListResponse = apiService.queryCameraList(token, cameraType, cameraId);
            // 检查相机信息响应是否为空
            if (cameraListResponse == null) {
                throw new CustomApiException("相机信息响应为空.");
            }
            return ResponseEntity.ok(cameraListResponse);
        } catch (CustomApiException e) {
            logger.error("查询相机信息时出错: {}", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            logger.error("发生意外错误: ", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("error", "An unexpected error occurred."));
        }
    }
    /*@Anonymous
    @GetMapping("/modifyCamera")
    public ResponseEntity<Map<String, Object>> modifyCamera() {
        try {
            // 直接从 TokenManager 获取 token
            String token = tokenManager.getToken();
            // 使用获取到的 token 添加和更新相机信息
            Map<String, Object> modifyCameraResponse = apiService.modifyCamera(token);
            // 检查相机信息响应是否为空
            if (modifyCameraResponse == null) {
                throw new CustomApiException("相机信息响应为空.");
            }
            return ResponseEntity.ok(modifyCameraResponse);
        } catch (CustomApiException e) {
            logger.error("添加和更新相机信息时出错: {}", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            logger.error("发生意外错误: ", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("error", "An unexpected error occurred."));
        }
    }*/
    /**
     * 脸库管理
     * * @author xhl
     * @return
     */
    @GetMapping("/queryFaceLibInfo")     //脸库详细信息获取
    public ResponseEntity<Map<String, Object>> queryFaceLibInfo() {
        try {
            // 直接从 TokenManager 获取 token
            String token = tokenManager.getToken();

            // 使用获取到的 token 查询脸库信息
            Map<String, Object> faceLibInfoResponse = apiService.queryFaceLibInfo(token);

            // 检查脸库信息响应是否为空
            if (faceLibInfoResponse == null) {
                throw new CustomApiException("人脸库信息响应为空.");
            }

            return ResponseEntity.ok(faceLibInfoResponse);
        } catch (CustomApiException e) {
            logger.error("查询面部库信息时出错: {}", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("error", e.getMessage()));
        } catch (Exception e) {
            logger.error("发生意外错误: ", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("error", "An unexpected error occurred."));
        }
    }
}




